package com.isoft.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.isoft.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;


@Mapper
public interface UserDao extends BaseMapper<User> {
    @Select("select * from AIRPORT.\"USER\" where NAME=#{name} and PASSWORD=#{password}")
    public User login(@Param("name") String name, @Param("password") String password);
    @Select("select * from AIRPORT.\"USER\" where NAME=#{acount} and \"IDENTITY\"=0")
    public List<User> isCommon(String account);

    @Select("select * from AIRPORT.\"USER\" where NAME=#{acount} and \"IDENTITY\"=1")
    public List<User> isAdmin(String account);

    @Select("select * from AIRPORT.\"USER\" where NAME=#{acount} and \"IDENTITY\"=2")
    public List<User> isAssessor(String account);
    @Insert("insert into AIRPORT.\"USER\"(NAME,PASSWORD,PHONE,ADDRESS,IDENTITY,ISLOCK,IdentifyingCode,EMAIL,SALT)" +
            "values (#{name},#{password},#{phone},#{address},0,0,#{IdentifyingCode},#{email},#{salt})")
    public int add(User user);

    @Select("select * from AIRPORT.\"USER\" WHERE ISLOCK=0")
    public IPage<User> getAll(Page<?> page);

    @Select("select * from AIRPORT.\"USER\" where ID=#{id}")
    public List<User> selectByid(Integer id) ;

    @Update("<script>" +
            "update AIRPORT.\"USER\" " +
            "        <set> " +
            "            <if test=\"null != name\"> " +
            "                \"NAME\"=#{name},                 " +
            "            </if> " +
            "            <if test=\"null !=phone\"> " +
            "                PHONE=#{phone}, " +
            "            </if> " +
            "            <if test=\"null != address\"> " +
            "                ADDRESS=#{address},      " +
            "            </if> "+
            "            <if test=\"null != identity\"> " +
            "                \"IDENTITY\"=#{identity},             " +
            "            </if> " +
            "        </set> " +
            "        where ID=#{id}"
            +"</script>")
    public int updateUserById(User user);
    @Update("update AIRPORT.\"USER\" set PASSWORD=#{password},SALT=#{salt} where ID=#{id}")
    public int updatePwd(@Param("id") Integer id ,@Param("password")String password,@Param("salt")String salt);
    @Select("select count(*)" +
            "from AIRPORT.\"USER\"" +
            "where NAME = #{name} AND ID !=#{id}")
    public int checkName(@Param("name") String name, @Param("id") int id);
    @Update("update AIRPORT.\"USER\" set PHOTOURL=#{photourl} where ID=#{id}  ")
    public int updatePhoto(Integer id,String photourl);

    @Delete("delete from AIRPORT.\"USER\" where ID=#{id}")
    public int deleteById(Integer id);
    @Select("SELECT * FROM AIRPORT.\"USER\" where NAME=#{name} AND ISLOCK=0")
    public User selectByName (String name);
}
